package com.lngsyz.eseal.base.biz.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lngsyz.eseal.base.entity.LoginInfo;
import com.lngsyz.eseal.base.entity.biz.InAddAppFile;
import com.lngsyz.eseal.base.entity.biz.InApplyReview;
import com.lngsyz.eseal.base.entity.biz.InGetRefuseReason;
import com.lngsyz.eseal.base.entity.biz.InGetTicket;
import com.lngsyz.eseal.base.entity.biz.InOpen;
import com.lngsyz.eseal.base.entity.biz.OutGetRefuseReason;
import com.lngsyz.eseal.base.entity.biz.OutGetTicket;
import com.lngsyz.eseal.base.entity.source1.TApp;
import com.lngsyz.eseal.base.entity.source1.TFile;
import com.lngsyz.eseal.base.exception.RequestSystemException;
import com.lngsyz.eseal.base.mapper.source1.TAppMapper;

@Service
public class TAppService {
    
	@Autowired
	TAppMapper tAppMapper;
	@Autowired
	TFileService tFileService;

	/**
	 * 新申请注册APP
	 * @param tApp
	 * @throws Exception
	 */
	public void addApp(TApp tApp, LoginInfo loginInfo) throws Exception {
		if(tApp != null) {
			//【1】基本信息审核
			tApp.setApplyPassFlag("0");
			tApp.setUserApplyPass("");
			tApp.setGmtUserApply(null);
			//【2】授权信息审核
//			tApp.setTicketFlag("0");
//			tApp.setUserTicket("");
//			tApp.setGmtTicket(null);
			//【3】启用标记
			tApp.setOpenFlag("0");
			tApp.setUserOpen("");
			tApp.setGmtOpen(null);
			//删除标记（0：正常 1：已删除）
			tApp.setDel("0");
			//【授权】
			tApp.setAppId("");
			tApp.setAppSecret("");
			//申请人
			tApp.setUserCreate(loginInfo.getId());
			tApp.setMobile(loginInfo.getMobile());
			tAppMapper.insert(tApp);
			
			tAppMapper.addFile(tApp);
		} else {
			throw new RequestSystemException("信息为空，无法注册");
		}
    }
	
	public int findReviewingCount(String userId) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("userCreate", userId);
		int count = tAppMapper.findReviewingCount(param);
		return count;
	}

	/**
	 * 查询记录
	 * @param inReviewAppInfo
	 * @return
	 * @throws Exception
	 */
	public List<TApp> findList(TApp tApp, int page, int size) throws Exception {
		Map<String, Object> aParam = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(tApp.getMobile())) {
			aParam.put("mobile", tApp.getMobile());
		}
		if(StringUtils.isNotBlank(tApp.getSysName())) {
			aParam.put("sysName", tApp.getSysName());
		}
		if(StringUtils.isNotBlank(tApp.getApplyName())) {
			aParam.put("applyName", tApp.getApplyName());
		}
		if(StringUtils.isNotBlank(tApp.getApplyDept())) {
			aParam.put("applyDept", tApp.getApplyDept());
		}
		if(StringUtils.isNotBlank(tApp.getApplyPassFlag())) {
			aParam.put("applyPassFlag", tApp.getApplyPassFlag());
		}
		if(StringUtils.isNotBlank(tApp.getOpenFlag())) {
			aParam.put("openFlag", tApp.getOpenFlag());
		}
		if(StringUtils.isNotBlank(tApp.getUserCreate())) {
			aParam.put("userCreate", tApp.getUserCreate());
		}
		aParam.put("minSize", page <= 0 ? 0 : (page-1) * size);
		aParam.put("maxSize", page <= 0 ? size : page * size);
		aParam.put("order", "GMT_CREATE DESC,ID");
		List<TApp> apps = tAppMapper.findByCondition(aParam);
		return apps;
	}
	
	/**
	 * 查询记录数量
	 * @param inReviewAppInfo
	 * @return
	 * @throws Exception
	 */
	public int findListCount(TApp tApp) throws Exception {
		Map<String, Object> aParam = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(tApp.getSysName())) {
			aParam.put("sysName", tApp.getSysName());
		}
		if(StringUtils.isNotBlank(tApp.getApplyName())) {
			aParam.put("applyName", tApp.getApplyName());
		}
		if(StringUtils.isNotBlank(tApp.getApplyDept())) {
			aParam.put("applyDept", tApp.getApplyDept());
		}
		if(StringUtils.isNotBlank(tApp.getApplyPassFlag())) {
			aParam.put("applyPassFlag", tApp.getApplyPassFlag());
		}
		if(StringUtils.isNotBlank(tApp.getOpenFlag())) {
			aParam.put("openFlag", tApp.getOpenFlag());
		}
		if(StringUtils.isNotBlank(tApp.getUserCreate())) {
			aParam.put("userCreate", tApp.getUserCreate());
		}
		int count = tAppMapper.findByConditionCount(aParam);
		return count;
	}
	
	public TApp findOne(String id) throws Exception {
		if(StringUtils.isNotBlank(id)) {
			TApp tApp = tAppMapper.findById(id);
			return tApp;
		} else {
			return null;
		}
	}
	
	public void applyPassReview(InApplyReview inApplyReview, LoginInfo loginInfo) throws Exception {
		String admin = loginInfo .getAdmin();
		if(StringUtils.equals(admin, "1")) {
			TApp tApp = findOne(inApplyReview.getId());
			if(tApp != null) {
				tApp.setApplyPassFlag(inApplyReview.getPassFlag());
				tApp.setUserApplyPass(loginInfo.getId());
				tApp.setApplyPassRemark(inApplyReview.getRemark());
				tAppMapper.applyPass(tApp);
			} else {
				throw new RequestSystemException("信息为空，无法审核");
			}
		} else {
			throw new RequestSystemException("无权操作");
		}
	}
	
//	public void ticketReview(InTicketReview inTicketReview, LoginInfo loginInfo) throws Exception {
//		String admin = loginInfo .getAdmin();
//		if(StringUtils.equals(admin, "1")) {
//			TApp tApp = findOne(inTicketReview.getId());
//			if(tApp != null) {
//				tApp.setTicketFlag(inTicketReview.getPassFlag());
//				tApp.setUserTicket(loginInfo.getId());
//				tApp.setTicketRemark(inTicketReview.getRemark());
//				tAppMapper.ticketPass(tApp);
//			} else {
//				throw new RequestSystemException("信息为空，无法审核");
//			}
//		} else {
//			throw new RequestSystemException("无权操作");
//		}
//	}
	
	public void open(InOpen inOpen, LoginInfo loginInfo) throws Exception {
		String admin = loginInfo .getAdmin();
		if(StringUtils.equals(admin, "1")) {
			TApp tApp = findOne(inOpen.getId());
			if(tApp != null) {
				tApp.setOpenFlag(inOpen.getOpenFlag());
				tApp.setUserOpen(loginInfo.getId());
				tAppMapper.open(tApp);
			} else {
				throw new RequestSystemException("信息为空，无法审核");
			}
		} else {
			throw new RequestSystemException("无权操作");
		}
	}
	
	public OutGetTicket getTicket(InGetTicket inGetTicket, LoginInfo loginInfo) throws Exception {
		TApp tApp = findOne(inGetTicket.getId());
		if(tApp != null && StringUtils.equals(loginInfo.getId(), tApp.getUserCreate())) {
			if(StringUtils.equals(tApp.getApplyPassFlag(), "1")) {
				if(StringUtils.isBlank(tApp.getAppId()) || StringUtils.isBlank(tApp.getAppSecret())) {
					String appId = "ttt";
					String appSecret = "135j3j45";
					tApp.setAppId(appId);
					tApp.setAppSecret(appSecret);
					tAppMapper.generateSecret(tApp);
					OutGetTicket rtn = new OutGetTicket();
					rtn.setAppId(appId);
					rtn.setAppSecret(appSecret);
					return rtn;
				} else {
					OutGetTicket rtn = new OutGetTicket();
					rtn.setAppId(tApp.getAppId());
					rtn.setAppSecret(tApp.getAppSecret());
					return rtn;
				}
			} else {
				throw new RequestSystemException("审核通过后即可获取");
			}
		} else {
			throw new RequestSystemException("信息为空，无法分配");
		}
	}
	
	public void addFiles(InAddAppFile inAddAppFile, LoginInfo loginInfo) throws Exception {
		String id = inAddAppFile.getId();
		TApp tApp = findOne(id);
		if(tApp != null) {
			if(StringUtils.equals(tApp.getApplyPassFlag(), "1")) {
				TFile tFile1 = tFileService.addFile(inAddAppFile.getFile1());
				TFile tFile2 = tFileService.addFile(inAddAppFile.getFile2());
				tApp.setFile1(tFile1.getId());
				tApp.setFile2(tFile2.getId());
				tAppMapper.addFile(tApp);
			} else {
				throw new RequestSystemException("需要优先通过基础信息审核");
			}
		} else {
			throw new RequestSystemException("未查询到申请信息");
		}
	}
	
	public OutGetRefuseReason getRefuseReason(InGetRefuseReason inGetRefuseReason) throws Exception {
		String id = inGetRefuseReason.getId();
		TApp tApp = findOne(id);
		if(tApp != null) {
			OutGetRefuseReason rtn = new OutGetRefuseReason();
			if(StringUtils.equals(tApp.getApplyPassFlag(), "2")) {
				rtn.setId(id);
				rtn.setStepName("基本信息审核");
				rtn.setPassFlagName("申请被驳回");
				rtn.setRemark(tApp.getApplyPassRemark());
				return rtn;
			} else {
				throw new RequestSystemException("未识别到驳回记录");
			}
		} else {
			throw new RequestSystemException("未查询到申请信息");
		}
	}
}
